<template>
    <div class="friends_list">
        <ul id="main" style="padding: 0;margin: 0">
            <li style="font-size: 0" v-for="(item,index) in searchedFriendlist" :key="index"
                @contextmenu.prevent="clickAttribute(index)"
                @click="goToFriendInfoPage(index)">
                <div class="list_title" v-if="item.initTitle">{{item.initTitle}}</div>
                <!--分割线-->
                <el-divider v-if="item.initTitle" direction="horizontal"></el-divider>
                <div class="friends" :class="{active: index === choiceIndex,activeRight: index === choiceRightIndex}">
                    <div class="list-left">
                        <el-avatar shape="square" style="width: 30px;height: 30px" :src="item.url" @error="true"
                                   onerror="this.src='static/images/vue.jpg'">
                            <img src="static/images/vue.jpg"/>
                        </el-avatar>
                    </div>
                    <div class="list-right">
                        <div class="title-info">
                            <p class="nickname">{{item.nickname}}</p>
                        </div>
                    </div>
                </div>
            </li>
        </ul>

        <!--    右键菜单配置    -->
        <context-menu class="right-menu"
                      :target="contextMenuTarget"
                      :show="contextMenuVisible"
                      @update:show="(show) => update(show)">
            <a href="javascript:;" @click="sendMsg">发消息</a>
            <a href="javascript:;" @click="share">分享给好友</a>
        </context-menu>
    </div>

</template>

<script>
  export default {
    name: 'friendlist',
    data () {
      return {
        contextMenuTarget: '', // 绑定的dom
        contextMenuVisible: false,
        searchedFriendlist: [
          {
            initTitle: 'A',
            id: 1,
            url: '',
            img: 'static/images/vue.jpg',
            nickname: '爱喝开水的小蜗牛'
          },
          {
            initTitle: '',
            id: 2,
            url: '',
            img: 'static/images/vue.jpg',
            nickname: 'A无名之辈'
          },
          {
            initTitle: 'B',
            id: 3,
            url: '',
            img: 'static/images/vue.jpg',
            nickname: '别摸我'
          },
          {
            initTitle: '',
            id: 4,
            url: '',
            img: 'static/images/vue.jpg',
            nickname: '爸爸'
          },
          {
            initTitle: '',
            id: 4,
            url: '',
            img: 'static/images/vue.jpg',
            nickname: '把心给你'
          }
        ],
        choiceIndex: -1,
        choiceRightIndex: -1
      }
    },
    mounted () {
      // 右击显示菜单 区域位置
      this.contextMenuTarget = document.getElementById('main')
    },
    methods: {
      goToFriendInfoPage (index) {
        this.choiceIndex = index
        console.log('进入好友信息页面')
      },
      sendMsg () {
        console.log('发送消息给' + this.choiceRightIndex + '的好友')
        this.update(false)
      },
      share () {
        console.log('将好友' + this.choiceRightIndex + '分享给其他好友')
        this.update(false)
      },
      update (show) {
        this.contextMenuVisible = show
        if (!show) {
          this.choiceRightIndex = -1
        }
      },
      clickAttribute (index) {
        console.log('点击了右键' + index)
        this.contextMenuVisible = true
        this.choiceRightIndex = index
      }
    }
  }
</script>

<style scoped>
    .friends_list {
        clear: both;
        overflow-y: auto;
        overflow-x: hidden;
        box-sizing: border-box;
    }

    .friends {
        display: flex;
        font-size: 0;
        border: 2px solid #f2f2f2;
    }

    .list_title {
        box-sizing: border-box;
        font-size: 12px;
        color: #999;
        display: flex;
        margin-left: 10px;
        margin-top: 10px;
        margin-bottom: 3px;
    }


    .list-left {
        margin: 5px;
    }

    .list-right {
        position: relative;
        flex: 1;
        margin: 10px;
    }

    .title-info {
        display: flex;
    }

    .nickname {
        margin: 0;
        flex: 1 1 auto;
        font-size: 14px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .el-divider--horizontal {
        margin: 0;
    }

    .active {
        background-color: #c4c4c4;
    }

    .activeRight {
        border: 2px solid #c4c4c4;
    }

    .right-menu {
        border: 1px solid #eee;
        box-shadow: 0 0.5em 1em 0 rgba(0, 0, 0, .1);
        border-radius: 1px;
        font-family: Microsoft Yahei, Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        position: fixed;
        background: #fff;
        z-index: 999;
    }

    .right-menu a {
        padding: 2px 15px;
        height: 28px;
        line-height: 28px;
        text-align: center;
        display: block;
        color: #1a1a1a !important;
    }

    .right-menu a:-webkit-any-link {
        color: -webkit-link;
        cursor: pointer;
        text-decoration: none;
    }

    .right-menu a:hover {
        background: #42b983;
        color: #fff !important;
    }

</style>
